var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		paypwd:'',
    hiddenmodalput: true,
		orderinfo: [],
	},
	_onLoad: function () {
		var that = this;
		$.get('wxpay/shoppay', { orderid: that.options.id }, function (data) {
			if (data.status == 0) {
				$.error(data.msg);
				return;
			}
			that.setData(data)
		})
	},
	getpwd:function(e){
		var paypwd = e.detail.value;
		this.setData({
			paypwd: paypwd
		});
	},
	cancel: function () {
		this.setData({
			hiddenmodalput: true
		});
	},
	modalinput: function () {
		this.setData({
			hiddenmodalput: !this.data.hiddenmodalput
		})
	},
	topay: function (e) {
		var that = this
		//var typeid = e.detail.value.paytype;
    var typeid = e.currentTarget.dataset.typeid;
		var orderid = this.data.orderinfo.id
		if (typeid == 2) {
			that.setData({hiddenmodalput:true})
			$.confirm('确定用'+(that.data.textset['余额'] || '余额')+'支付吗?', function () {
				$.post('wxpay/shoppay', {orderid: orderid,typeid: typeid,paypwd:that.data.paypwd}, function (data) {
					if (data.status == 0) {
						$.error(data.msg);
						return;
					}
					if (data.status == 2) {
						$.success(data.msg);
						that.subscribeMessage(function(){
							setTimeout(function () {
								if(that.data.give_coupon_list && that.data.give_coupon_list.length > 0){
									that.setData({give_coupon_show:true,give_coupon_close_url:'/pages/my/usercenter'})
								}else{
									$.goto('/pages/my/usercenter');
								}
							},1000);
						})
						return;
					}
				}, '提交中');
			})
		} else if(typeid == 3) {
			$.confirm('确定要'+that.data.codtxt+'吗?', function () {
				$.post('wxpay/shoppay', {orderid: orderid,typeid: typeid}, function (data) {
					if (data.status == 0) {
						$.error(data.msg);
						return;
					}
					if (data.status == 2) { //无需付款
						$.success(data.msg);
						that.subscribeMessage(function(){
							setTimeout(function () {
								$.goto('/pages/my/usercenter');
							}, 1000);
						})
						return;
					}
				}, '提交中');
			})
		}else {
			$.post('wxpay/shoppay', {orderid: orderid,typeid: typeid}, function (data) {
				if (data.status == 0) {
					$.error(data.msg);
					return;
				}
				if (data.status == 2) { //无需付款
					$.success(data.msg);
					that.subscribeMessage(function(){
						setTimeout(function () {
							if(that.data.give_coupon_list && that.data.give_coupon_list.length > 0){
								that.setData({give_coupon_show:true,give_coupon_close_url:'/pages/my/usercenter'})
							}else{
								$.goto('/pages/my/usercenter');
							}
						}, 1000);
					})
					return;
				}
				var opt = data.data;
				if($.platform == 'wx'){
					wx.requestPayment({
						'timeStamp': opt.timeStamp,
						'nonceStr': opt.nonceStr,
						'package': opt.package,
						'signType': 'MD5',
						'paySign': opt.paySign,
						'success': function (res) {
							$.success('付款完成');
							that.subscribeMessage(function(){
								setTimeout(function () {
									if(that.data.give_coupon_list && that.data.give_coupon_list.length > 0){
										that.setData({give_coupon_show:true,give_coupon_close_url:'/pages/my/usercenter'})
									}else{
										$.goto('/pages/my/usercenter');
									}
								}, 1000)
							})
						},
						'fail': function (res) { }
					})
				}
			}, '提交中');
		}
	}
}))